package com.gsoe.problem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gsoe.problem.po.Test;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 测验表 Mapper 接口
 * </p>
 *
 * @author xiaoma
 * @since 2024-10-27
 */
public interface TestMapper extends BaseMapper<Test> {

    @Insert("INSERT INTO test (test_name, user_id, user_name, create_time, score, status,time) " +
            "SELECT #{test.testName}, #{test.userId}, #{test.userName}, #{test.createTime}, #{test.score}, #{test.status},#{test.time} " +
            "FROM (SELECT 1) AS dummy " +
            "WHERE NOT EXISTS (" +
            "  SELECT 1 " +
            "  FROM test " +
            "  WHERE test_name = #{test.testName} AND user_id = #{test.userId}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer insertWithLock(@Param("test") Test test);


    @Update("update test set status=2 where id=#{textId} and status=1")
    Integer updateStatusByLock(@Param("textId") Long textId);
}
